

global filename jmp_results_fs_new_iv


use  "stata data/regsave/A1_new_first_stage", clear
append using "stata data/regsave/A1_new_coastal_first_stage"
append using "stata data/regsave/A1_new_inland_first_stage"



duplicates drop
order cz


capture order type column 
gen temp_term = regexm(var,"temp")


//here I drop the pdp one in the marginal since the marginal effect comes from the pdp_non_event variable


replace var = "pdp" if var == "pdp_event_day"


drop tstat	ci_lower	ci_upper	N	r2

gen stars = ""
replace stars = "*" if pval<.1
replace stars = "**" if pval<.05
replace stars = "***" if pval<.01

gen label1 = "Eligible $\times$ Post"

replace label1 = "Temperature" if var == "temp_f"
replace label1 = "Temperature squared" if var == "temp_f_2"




capture file close table1		
file open table1 using "latex/$filename.tex", write text replace

*file write table1 "\begin{table}[htbp]\centering" _n
*file write table1 "\begin{threeparttable}" _n //this wraps the caption
file write table1 "\label{$filename}" _n
file write table1 "\vspace{-2mm}" _n
*file write table1 "\footnotesize" _n
*file write table1 "\begin{tabular}{lcccccc}" _n //this is where you change justification. Options are l c r

file write table1 "\begin{tabular}{@{\extracolsep{4pt}}lcc@{\hskip .5cm}cc@{\hskip .5cm}cc}" _n //this is where you change justification. Options are l c r
*extracolsep from http://tex.stackexchange.com/questions/101000/how-to-shorten-cline-so-that-multiple-groups-in-a-table-become-visible

file write table1 "\hline\hline \\ [-.9em]" _n 

*file write table1 "\multicolumn{1}{c}{$\begin{matrix}\text{Baseline}\\ \text{Territory} \end{matrix}$}& 	\multicolumn{1}{c}{$\begin{matrix}\text{Average}\\ \text{Temperature} \end{matrix}$} &	\multicolumn{1}{c}{$\begin{matrix}\text{Firm}\\ \text{Count} \end{matrix}$}	&	\\" _n
*file write table1 " Energy use classification && Median & & &Thirds	\\" _n 
*file write table1 "  \cline{2-4} \cline{5-7} \\" _n 
*file write table1 "[0.2em]" _n

*file write table1 "  & \multicolumn{1}{c}{ All PG\&E} & \multicolumn{1}{c}{Coastal} & \multicolumn{1}{c}{Inland} \\" _n 


*file write table1 "  && Structural winners & & &Structural losers\\" _n 
*file write table1 "  \cline{2-3} \cline{4-5} \cline{6-7} \\" _n 
file write table1 "  & (1) & (2) & (3)  \\" _n
file write table1 "[0.2em]" _n
file write table1 "  & All PG\&E  & Coastal & Inland \\" _n
file write table1 "[0.1em]" _n
file write table1 "\hline \\" _n
file write table1 "[-0.7em]" _n



capture gen row = _n

gen avg_kwh_high_EI_label = ""


gen column_id = cz 
order column_id


local col1 "all"
local col2 "coastal"
local col3 "inland"




foreach v in "eligible" "temp_f" "temp_f_2" { //this first loop looks through pdp_high_EIs producing and services providing

foreach col in "`col1'" "`col2'" "`col3'"  { //"`col2'" "`col3'" "`col4'" "`col5'" "`col6'" { //this loop loops through event day and non-event day

local panel "event"  // "non_event" "marginal"{ //this loop loops through event day and non-event day

sum row if var == "`v'" & col == "`col'" & type == "`panel'" //this grabs the correct row. So goods event is row number ___ - I then grab that NUMBER observation out of variable
local r = r(mean) //just a way to indicate what row you're on from the sum term above


local name_`col'_`panel' = label1[`r'] 
local c1_`col'_`panel' = "\$"+string(coef[`r'],"%9.5f")+ "$" + stars[`r'] 
local c2_`col'_`panel' = "(" +"\$"+ string(stderr[`r'],"%9.5f")+"$"  + ")"

local avg_kwh_high_EI_label = avg_kwh_high_EI_label[`r'] 
*local s1_`col'_`panel' = string(avg_kwh_high_EI[`r'],"%-9.2f")
}

file write table1 "`name_`col1'_`panel'' & `c1_`col1'_`panel'' & `c1_`col2'_`panel'' & `c1_`col3'_`panel''      \\" _n //the COEF line. Include title
file write table1 "		 & `c2_`col1'_`panel'' & `c2_`col2'_`panel'' & `c2_`col3'_`panel''   \\" _n //the standard error line. The & at the front is b/c no titles on this line
file write table1 "[1.5ex] " _n //this adds vertical space between the se/coef groupings

}

///
///Marginal
///



gen firms_label = "Establishments"
gen event_day_kwh_label = "Event day kWh usage"

capture drop money_pdp_saved
gen money_pdp_saved = coef * 4 * .6 * -1
gen money_pdp_saved_label = "Dollar savings"




foreach v in "eligible"  { //this first loop looks through pdp_high_EIs producing and services providing

foreach col in "`col1'" "`col2'" "`col3'"  { //  "`col3'" "`col4'" "`col5'" "`col6'" { //this loop loops through event day and non-event day

sum row if var == "eligible" & col == "`col'" & type == "event" //this grabs the correct row. So goods event is row number ___ - I then grab that NUMBER observation out of variable
local r = r(mean) //just a way to indicate what row you're on from the sum term above



local firms_label = firms_label[`r'] 
local s2_`col' = "\$" + string(firms[`r'],"%-9.0fc") +"$"

*local event_day_kwh_label = event_day_kwh_label[`r'] 
*local s3_`col' = "\$" + string(avg_kwh[`r'],"%-9.2fc") +"$"


local s4_`col' = "\$" + string(f[`r'],"%-9.0fc") +"$"


}
*file write table1 "`firms_label' & `s1_5' & `s1_8'  \\" _n 
file write table1 "`firms_label' 		 & `s2_`col1'' & `s2_`col2'' & `s2_`col3''  \\" _n 
*file write table1 "`event_day_kwh_label' & `s3_`col1'' & `s3_`col2'' & `s3_`col3'' & `s3_`col4'' & `s3_`col5'' & `s3_`col6'' \\" _n 
file write table1 " F statistic 		 & `s4_`col1'' & `s4_`col2'' & `s4_`col3''  \\" _n 


}
*




file write table1 "\hline" _n
file write table1 "\end{tabular}" _n
file close table1		

